package com.dc.controller;

import com.dc.baseController.BaseController;
import com.dc.em.ErrorMessageEnum;
import com.dc.model.Shoes;
import com.dc.model.User;
import com.dc.service.ShoesService;
import com.dc.vo.BaseModel;
import com.dc.vo.BusinessException;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;

@Api("商品模块")
@Controller
@RequestMapping("/Shoes")
public class ShoesController extends BaseController {
    @Autowired
    ShoesService shoesService;
    @RequestMapping(value = "/selectPageShoes",method = RequestMethod.GET)
    @ApiOperation(value = "分页查询商品信息",httpMethod = "GET",notes = "查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页面",name = "curr_page",defaultValue = "1",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "一页的最大条数",name = "page_size",defaultValue = "10",dataType = "Integer",paramType = "query")
    })
    @ResponseBody
    public BaseModel selectPageShoes (Integer curr_page, Integer page_size, BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return shoesService.selectPageShoes(curr_page, page_size, baseModel);
    }
    @RequestMapping(value = "/addShoes",method = RequestMethod.POST)
    @ApiOperation(value = "新增商品",httpMethod = "POST",notes = "新增")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "商品名称",name = "shoes_name",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "商品图片",name = "shoes_img",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "商品价格",name = "price",required = true,dataType = "BigDecimal",paramType = "query"),
            @ApiImplicitParam(value = "商品详情",name = "memo",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "商品类型",name = "category_id",required = true,dataType = "Integer",paramType = "query")
    })
    @ResponseBody
    public BaseModel addShoes(Shoes shoes,BaseModel baseModel,HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        shoes.setCreate_user_id(user.getUser_id());
        shoes.setIs_deleted(0);
        shoes.setVersion(0);
        return shoesService.addShoes(shoes,baseModel);
    }
    @RequestMapping(value = "/deletedShoes/{ids}",method = RequestMethod.POST)
    @ApiOperation(value="删除商品",httpMethod = "POST",notes = "删除")
    @ResponseBody
    public BaseModel deletedShoes(@PathVariable("ids") String ids, BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return shoesService.deletedShoes(ids,baseModel);
    }
    @RequestMapping(value = "/selectShoesById/{id}",method = RequestMethod.POST)
    @ApiOperation(value = "根据id查询商品",httpMethod = "POST",notes = "查询")
    @ResponseBody
    public BaseModel selectShoesById(@PathVariable("id") Integer id,BaseModel baseModel,HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        System.out.println(user+"1111111");
        if(user == null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return shoesService.selectShoesById(id,baseModel);
    }
    @RequestMapping(value = "/selectShoesByName",method = RequestMethod.POST)
    @ApiOperation(value = "根据商品名称查询信息",httpMethod = "POST",notes = "查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "商品名称",name = "shoes_name",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "当前页面",name = "curr_page",defaultValue = "1",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "一页的最大条数",name = "page_size",defaultValue = "10",dataType = "Integer",paramType = "query")
    })
    @ResponseBody
    public BaseModel selectShoesByName (String shoes_name,Integer curr_page, Integer page_size, BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return shoesService.selectShoesByName(shoes_name,curr_page, page_size, baseModel);
    }
    @RequestMapping(value = "/updateShoes",method = RequestMethod.POST)
    @ApiOperation(value = "修改商品",httpMethod = "POST",notes = "修改")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "商品ID",name = "shoes_id",required = true,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "商品名称",name = "shoes_name",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "商品图片",name = "shoes_img",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "商品价格",name = "price",required = false,dataType = "BigDecimal",paramType = "query"),
            @ApiImplicitParam(value = "商品详情",name = "memo",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "商品类型",name = "category_id",required = false,dataType = "Integer",paramType = "query")
    })
    @ResponseBody
    public BaseModel updateShoes(Shoes shoes, BaseModel baseModel, HttpServletRequest request, MultipartFile file) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        String path = request.getSession().getServletContext().getRealPath("/images");
        System.out.println("路径："+path);
        String fileName = file.getOriginalFilename();
        System.out.println("文件名称"+fileName);
        File dir = new File(path, fileName);
        System.out.println("判断目录是否存在："+dir.exists());
        if(!dir.exists()){
            dir.mkdirs();
        }
//      MultipartFile自带的解析方法
        file.transferTo(dir);
        //这里我在拼接出我图片要显示的url
        String imageurl = "http://localhost:9527/shoes_Web_exploded/images/"+fileName;
        shoes.setShoes_img(imageurl);
        return shoesService.updateShoes(shoes,baseModel);
    }
    @RequestMapping(value = "/updateShoess",method = RequestMethod.POST)
    @ApiOperation(value = "修改商品",httpMethod = "POST",notes = "修改")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "商品ID",name = "shoes_id",required = true,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "商品名称",name = "shoes_name",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "商品图片",name = "shoes_img",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "商品价格",name = "price",required = false,dataType = "BigDecimal",paramType = "query"),
            @ApiImplicitParam(value = "商品详情",name = "memo",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "商品类型",name = "category_id",required = false,dataType = "Integer",paramType = "query")
    })
    @ResponseBody
    public BaseModel updateShoes(Shoes shoes, BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return shoesService.updateShoes(shoes,baseModel);
    }
    @RequestMapping(value = "/selectShoesByCategory",method = RequestMethod.POST)
    @ApiOperation(value = "分类查询商品信息",httpMethod = "POST",notes = "查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "类别ID",name = "category_id",defaultValue = "1",dataType = "int",paramType = "query"),
            @ApiImplicitParam(value = "当前页面",name = "curr_page",defaultValue = "1",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "一页的最大条数",name = "page_size",defaultValue = "8",dataType = "Integer",paramType = "query")
    })
    @ResponseBody
    public BaseModel selectShoesByCategory (int category_id,Integer curr_page, Integer page_size, BaseModel baseModel, HttpServletRequest request) throws Exception{
        return shoesService.selectShoesByCategory(category_id,curr_page, page_size, baseModel);
    }
}
